package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.RoomInfoService;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private RoomAttrValueMapper roomAttrValueMapper;
    @Autowired
    private RoomFacilityMapper roomFacilityMapper;
    @Autowired
    private RoomLabelMapper roomLabelMapper;
    @Autowired
    private RoomPaymentTypeMapper roomPaymentTypeMapper;
    @Autowired
    private RoomLeaseTermMapper roomLeaseTermMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Override
    @Transactional
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {
        Long roomId = roomSubmitVo.getId();
        this.saveOrUpdate(roomSubmitVo);
        if (roomId != null) {
            //删除图片信息
            QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
            graphInfoQueryWrapper.eq("item_type", ItemType.ROOM);
            graphInfoQueryWrapper.eq("item_id", roomId);
            graphInfoMapper.delete(graphInfoQueryWrapper);
            //删除属性信息
            QueryWrapper<RoomAttrValue> roomAttrValueQueryWrapper = new QueryWrapper<>();
            roomAttrValueQueryWrapper.eq("room_id", roomId);
            roomAttrValueMapper.delete(roomAttrValueQueryWrapper);
            //删除配套信息
            QueryWrapper<RoomFacility> roomFacilityQueryWrapper = new QueryWrapper<>();
            roomFacilityQueryWrapper.eq("room_id", roomId);
            roomFacilityMapper.delete(roomFacilityQueryWrapper);
            //删除标签信息
            QueryWrapper<RoomLabel> roomLabelQueryWrapper = new QueryWrapper<>();
            roomLabelQueryWrapper.eq("room_id", roomId);
            roomLabelMapper.delete(roomLabelQueryWrapper);
            //删除支付方式信息
            QueryWrapper<RoomPaymentType> roomPaymentTypeQueryWrapper = new QueryWrapper<>();
            roomPaymentTypeQueryWrapper.eq("room_id", roomId);
            roomPaymentTypeMapper.delete(roomPaymentTypeQueryWrapper);
            //删除可选租期信息
            QueryWrapper<RoomLeaseTerm> roomLeaseTermQueryWrapper = new QueryWrapper<>();
            roomLeaseTermQueryWrapper.eq("room_id", roomId);
            roomLeaseTermMapper.delete(roomLeaseTermQueryWrapper);
        }
        roomId = roomSubmitVo.getId();
        //批量插入图片信息
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        graphInfoMapper.insertBatch(roomId, ItemType.ROOM, graphVoList);
        //批量插入属性信息
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        roomAttrValueMapper.insertBatch(roomId, attrValueIds);
        //批量插入配套信息
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        roomFacilityMapper.insertBatch(roomId, facilityInfoIds);
        //批量插入标签信息
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        roomLabelMapper.insertBatch(roomId, labelInfoIds);
        //批量插入支付方式信息
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        roomPaymentTypeMapper.insertBatch(roomId, paymentTypeIds);
        //批量插入可选租期信息
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        roomLeaseTermMapper.insertBatch(roomId, leaseTermIds);
    }

    @Override
    public IPage<RoomItemVo> pageRoomItemByQuery(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pageRoomItemByQuery(page, queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        //获取房间信息
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        //获取图片信息
        QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
        graphInfoQueryWrapper.eq("item_type", ItemType.ROOM);
        graphInfoQueryWrapper.eq("item_id", id);
        List<GraphInfo> graphInfos = graphInfoMapper.selectList(graphInfoQueryWrapper);
        List<GraphVo> graphVos = graphInfos.stream().map(graphInfo -> {
            GraphVo graphVo = new GraphVo();
            BeanUtils.copyProperties(graphInfo, graphVo);
            return graphVo;
        }).collect(Collectors.toList());
        //获取公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        //获取属性信息
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectListByRoomId(id);
        //获取配套信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByRoomId(id);
        //获取标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByRoomId(id);
        //获取支付方式信息
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectListByRoomId(id);
        //获取可选租期信息
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectListByRoomId(id);
        roomDetailVo.setGraphVoList(graphVos);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        return roomDetailVo;
    }

    @Transactional
    @Override
    public void removeRoomById(Long id) {

        //删除图片信息
        QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
        graphInfoQueryWrapper.eq("item_type", ItemType.ROOM);
        graphInfoQueryWrapper.eq("item_id", id);
        graphInfoMapper.delete(graphInfoQueryWrapper);
        //删除属性信息
        QueryWrapper<RoomAttrValue> roomAttrValueQueryWrapper = new QueryWrapper<>();
        roomAttrValueQueryWrapper.eq("room_id", id);
        roomAttrValueMapper.delete(roomAttrValueQueryWrapper);
        //删除配套信息
        QueryWrapper<RoomFacility> roomFacilityQueryWrapper = new QueryWrapper<>();
        roomFacilityQueryWrapper.eq("room_id", id);
        roomFacilityMapper.delete(roomFacilityQueryWrapper);
        //删除标签信息
        QueryWrapper<RoomLabel> roomLabelQueryWrapper = new QueryWrapper<>();
        roomLabelQueryWrapper.eq("room_id", id);
        roomLabelMapper.delete(roomLabelQueryWrapper);
        //删除支付方式信息
        QueryWrapper<RoomPaymentType> roomPaymentTypeQueryWrapper = new QueryWrapper<>();
        roomPaymentTypeQueryWrapper.eq("room_id", id);
        roomPaymentTypeMapper.delete(roomPaymentTypeQueryWrapper);
        //删除可选租期信息
        QueryWrapper<RoomLeaseTerm> roomLeaseTermQueryWrapper = new QueryWrapper<>();
        roomLeaseTermQueryWrapper.eq("room_id", id);
        roomLeaseTermMapper.delete(roomLeaseTermQueryWrapper);
        //删除房间信息
        this.removeById(id);
    }
}




